﻿using UnityEngine;
using System.Collections;
using System;
using System.Text;

public class Oc_ItemListItem : ObjectBase {

    const int RareMax = 6;
  
    [NonSerialized]
    public Item m_Item;
    bool m_IsOn = false;

    public GameObject m_OnPanel;
    public GameObject m_OffPanel;

    public GameObject m_Button_ItemSale;
    public EPButton m_Button_ItemEquip;

    public UILabel m_Kina;
    public UILabel m_Lavel;
    public UILabel m_ItemName;
    public UILabel m_Strengthen;

    public Transform m_RareRoot;
    Transform[] m_Rare;
    public GameObject[] m_Hand;

    public UISprite m_ItemSp;

    bool m_IsSetDetail = false;     // 디테일 설명 세팅했는지

	public void SetItem(Item item, MemberModel model)
    {       
        m_Item = item;

        m_Kina.text = string.Format("{0:#,0}", m_Item.itemInfo.price);

        if (item.itemInfo.itemType == 0)        // 장착 가능 아이템
        {
            if (model.level < m_Item.itemInfo.level)
            {
                m_Lavel.text = string.Format("[ff0000]Lv {0}[-]", m_Item.itemInfo.level);
                m_Button_ItemEquip.isEnabled = false;
            }
            else
            {
                m_Lavel.text = string.Format("Lv {0}", m_Item.itemInfo.level);
                m_Button_ItemEquip.isEnabled = true;
            }
        }
        else        
            m_Button_ItemEquip.isEnabled = false;       
        
        m_ItemName.text = TextController.GetStringWithId(string.Format("itemName{0}", m_Item.itemInfo.itemID));
        m_ItemName.MakePixelPerfect();

        if (m_Item.level > 0)
            m_Strengthen.text = string.Format("+{0}", m_Item.level);
        else
            m_Strengthen.text = "";

      
        if(m_Rare == null)
        {
            m_Rare = new Transform[RareMax];
            for(int i = 0 ; i < RareMax ; i++)
            {
                m_Rare[i] = m_RareRoot.Find(string.Format("{0}", i + 1));
            }
        }

        for (int i = 0; i < RareMax; i++)
        {
            m_Rare[i].gameObject.SetActive(m_Item.itemInfo.rarity == i + 1);
        }

        //float startPosX = StarCenter - ((StarGap * (m_Item.itemInfo.rarity - 1)) / 2);

        //for (int i = 0; i < RareMax; i++)
        //{
        //    if (i < m_Item.itemInfo.rarity)
        //    {
        //        Vector3 starPos = m_Stars[i].transform.localPosition;
        //        starPos.x = startPosX + (StarGap * i);
        //        m_Stars[i].transform.localPosition = starPos;

        //        m_Stars[i].SetActive(true);
        //    }
        //    else
        //        m_Stars[i].SetActive(false);
        //}

        m_Hand[0].SetActive(m_Item.itemInfo.part == 1); // 오른손 왼손 표시
        m_Hand[1].SetActive(m_Item.itemInfo.part == 2);

        m_IsSetDetail = false;  //아이템 바뀌면 디테일 다시 설정 필요

        m_Button_ItemSale.SetActive(m_Item.itemInfo.sell == 1); // 0 안팜 1 팜

        WeaponAtlasController.SetWeaponSprite(m_ItemSp, m_Item);
        //m_ItemSp.spriteName = TextController.GetItemDefine(m_Item);

        UISprite itemBg = m_ItemSp.transform.GetChild(0).GetComponent("UISprite") as UISprite;
        itemBg.color = Constants.GetItemBGRareColorValue(m_Item.itemInfo.rarity);
    }

    public void SetOn(bool isOn)
    {
        m_IsOn = isOn;

        m_OnPanel.SetActive(m_IsOn);
        m_OffPanel.SetActive(!m_IsOn);

        if(isOn && !m_IsSetDetail)
        {
            SetDetail();
            m_IsSetDetail = true;
        }
    }


    void SetDetail()
    {
        //float startPosX = StarCenter - ((StarGap * (m_Item.itemInfo.rarity - 1)) / 2);

        //for (int i = 0; i < RareMax; i++)
        //{
        //    if (i < m_Item.itemInfo.rarity)
        //    {
        //        Vector3 starPos = m_Stars[i].transform.localPosition;
        //        starPos.x = startPosX + (StarGap * i);
        //        m_Stars[i].transform.localPosition = starPos;

        //        m_Stars[i].SetActive(true);
        //    }
        //    else
        //        m_Stars[i].SetActive(false);
        //}

        Transform textTr = m_OnPanel.transform.Find("Text");
        Transform kindTr = textTr.transform.Find("01");
        UILabel tempLabel = null;
        
        float gap = 50.0f;
        if (m_Item.itemInfo.effectType1 > 0)
        {
            Transform labelTR = kindTr.Find("name1");
            tempLabel = labelTR.GetComponent("UILabel") as UILabel;
            tempLabel.text = TextController.GetStringWithId(string.Format("combatStat{0}", m_Item.itemInfo.effectType1));

            float size = tempLabel.width;

            tempLabel = kindTr.Find("value1").GetComponent("UILabel") as UILabel;
            tempLabel.text = string.Format("+{0}", m_Item.effectValue01);

            Vector3 pos = tempLabel.transform.localPosition;
            pos.x = labelTR.localPosition.x + size + gap;
            tempLabel.transform.localPosition = pos;
        }
        else
        {
            tempLabel = kindTr.Find("name1").GetComponent("UILabel") as UILabel;
            tempLabel.text = "";

            tempLabel = kindTr.Find("value1").GetComponent("UILabel") as UILabel;
            tempLabel.text = "";
        }

        if (m_Item.itemInfo.effectType2 > 0)
        {
            Transform labelTR = kindTr.Find("name2");
            tempLabel = labelTR.GetComponent("UILabel") as UILabel;
            tempLabel.text = TextController.GetStringWithId(string.Format("combatStat{0}", m_Item.itemInfo.effectType2));

            float size = tempLabel.width;

            tempLabel = kindTr.Find("value2").GetComponent("UILabel") as UILabel;
            tempLabel.text = string.Format("+{0}", m_Item.effectValue02);

            Vector3 pos = tempLabel.transform.localPosition;
            pos.x = labelTR.localPosition.x + size + gap;
            tempLabel.transform.localPosition = pos;
        }
        else
        {
            tempLabel = kindTr.Find("name2").GetComponent("UILabel") as UILabel;
            tempLabel.text = "";

            tempLabel = kindTr.Find("value2").GetComponent("UILabel") as UILabel;
            tempLabel.text = "";
        }        

        kindTr = textTr.transform.Find("02");
        tempLabel = kindTr.Find("value").GetComponent("UILabel") as UILabel;
        tempLabel.text = string.Format("{0}", m_Item.level);
        tempLabel = kindTr.Find("max").GetComponent("UILabel") as UILabel;
        tempLabel.text = string.Format("/ {0}", m_Item.itemInfo.upgradeMax);

        kindTr = textTr.transform.Find("03");
        tempLabel = kindTr.Find("class").GetComponent("UILabel") as UILabel;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < m_Item.itemInfo.itemClass.Length; i++)
        {
            sb.Append(TextController.GetItraitName(m_Item.itemInfo.itemClass[i]));
            if (i < m_Item.itemInfo.itemClass.Length - 1)
                sb.Append(", ");
        }
        tempLabel.text = sb.ToString();

        kindTr = textTr.transform.Find("04");
        kindTr.gameObject.SetActive(false);
        //tempLabel = kindTr.Find("species").GetComponent("UILabel") as UILabel;
        //sb = new StringBuilder();
        //for (int i = 0; i < m_Item.itemInfo.itemClass.Length; i++)
        //{
        //    sb.Append(TextController.GetItraitName(m_Item.itemInfo.itemClass[i]));
        //    if (i < m_Item.itemInfo.itemClass.Length - 1)
        //        sb.Append(", ");
        //}
        //tempLabel.text = sb.ToString();
    }
}
